<template>
	<view class="imgCont" style="border: none;">
		<view class="main">
			<!-- 一级分类 -->
			<!-- <view class="active_tit paddingNone">
				<view class="imgTit">
				<slot></slot>	借款人基本信息影像资料
				</view>
				<view class="imgBtn" @click="isShow">
					{{showText}}
				</view>
			</view> -->
			<!-- 二级分类 -->
			<block >
				<block v-for="item in treeData">
					<view class="active_tit paddingNone" >
						<view class="imgTit2">
							{{item.label}}
						</view>
						<view class="imgBtn" @click="isShowItem(item)">
							{{item.showText}}
						</view>
					</view>
					<block v-if="item.show">
						<block v-for="zitem in item.children">
							<view class="active_tit paddingNone">
								<view class="imgTit3">
									{{zitem.label}}
								</view>
							</view>
							<block v-for="imgItem in imageData[zitem.code]">
								<view class="choose-img-box" v-if="imgItem.base64!=''">
									<icon v-if="imgItem.base64!=''" class="icon" type="cancel" size="26"
										@click="remove(imageData[zitem.code],imgItem,zitem.code)" />
									<image class="img" :src="imgItem.img" @click="lookImg(imgItem.img)" />
								</view>
							
							</block>
							<view class="choose-img-box">
								<image class="img" src="/static/enterprise_card.png"
									@click="chooseImg(imageData[zitem.code],zitem.code)" />
							</view>
						</block>
						
					</block>
					
					
				</block>
			</block>
			

			

		</view>
		

	</view>

</template>

<script>
	import chooseImgToBase64 from "@/utils/chooseImgToBase64";
	export default {
		data() {
			return {
				showText: '展开',
				show: false,
				treeData: [{
						label: '合同材料',
						value: '',
						showText:'展开',
						children: [{
								label: '借款人还款账户材料（卡号）',
								value: '',
								code:'SDMS_0701_44'
							},
							{
								label: '受托支付申请材料（自主支付申请书）',
								value: '',
								code:'SDMS_0701_47'
							},
							{
								label: '受托支付材料',
								value: '',
								code:'SDMS_0701_48'
							},
							{
								label: '其他材料',
								value: '',
								code:'SDMS_0701_42'
							},
						]
					}
				],
				imageData: {
					SDMS_0701_44:[{img:'/static/enterprise_card.png',base64:''}],
					SDMS_0701_47:[{img:'/static/enterprise_card.png',base64:''}],
					SDMS_0701_48:[{img:'/static/enterprise_card.png',base64:''}],
					SDMS_0701_42:[{img:'/static/enterprise_card.png',base64:''}],
				}
			}
		},
		props: {
			changeFlag: {
				type: Number
			},
		},
		computed: {

		},
		watch: {
			changeFlag: {
				handler: function(res) {
					console.warn('数据变化2222222222222222',res)
					this.imageData = getApp().globalData.imageData.contract
				},
				deep: true
			},
			imageData: {
				handler: function(res) {
					console.warn('数据变化1111111111111111111111',)
					getApp().globalData.imageData.contract = res
				},
				deep: true
			}
		},
		onShow() {

		},
		mounted() {
			this.fileQuery('SDMS_0701')
		},
		methods: {
			fileQuery(code) {

				let obj = {
					typeCode: code,
					businessId: getApp().globalData.stdOffsurBaiscInfo.businessId,
				}
				this.sendRequest({
					url: 'ext/findImageList', //'offsur/fileQuery',
					method: 'post',
					hideLoading: true,
					data: obj,
					complete: (res) => {
						let list = Object.keys(this.imageData)
						let imgData = []
						if (res.code == '200') {
							res.data.map((item, key) => {
								item.img = 'data:image/png;base64,' + item.fileBase64
								item.base64 = item.fileBase64
							})

							//console.log(res.data)

							for (let i = 0; i < res.data.length; i++) {
								for (let j = 0; j < list.length; j++) {
									if (res.data[i].typeCode == list[j]) {
										this.imageData[list[j]].push(res.data[i])
									}
								}
							}

						}
					}
				})
			},
			chooseImg(name, code) {

				chooseImgToBase64(
					(base64, base64SourceData, file) => {
						//console.log(base64SourceData)
						let imgData = {
							img: base64,
							base64: base64SourceData
						}

						//this.idCardOcr("FRONT");
						this.fileUpload(code, file.name, base64SourceData, imgData, name)

					}, 2048

				);
			},
			fileUpload(code, fileName, base64, item, name) {
				let obj = {
					fileBase64: base64,
					businessId: getApp().globalData.stdOffsurBaiscInfo.businessId,
					typeCode: code,
					fileName: fileName,
					currentUserId: getApp().globalData.stdOffsurBaiscInfo.customerId
				}
				this.sendRequest({
					url: 'ext/upload',
					method: 'post',
					hideLoading: false,
					data: obj,
					complete: (res) => {

						if (res.code == '200') {
							item.fileId = res.data.fileId
							console.log(name)
							if(!name){
								this.imageData[code] = []
								this.imageData[code].push(item)
								console.log(this.imageData[code])
							}else{
								name.push(item)
							}
							
						} else {
							uni.showToast({
								icon: 'none',
								title: "图片上传失败，请重试",
								duration: 2000
							})
						}
					}
				})
			},


			remove(name, item) {
				let obj = {
					businessId:getApp().globalData.stdOffsurBaiscInfo.businessId,
					fileId: item.fileId,
					currentUserId: getApp().globalData.stdOffsurBaiscInfo.customerId
				}
				this.sendRequest({
					url: 'ext/deleteImage',
					method: 'post',
					hideLoading: true,
					data: obj,
					complete: (res) => {

						if (res.code == '200') {
							name.remove(item)
						}
					}
				})

			},
			isShow() {
				if (this.show) {
					this.show = false
					this.showText = '展开'
				} else {
					this.show = true
					this.showText = '收起'
				}
			},
			isShowItem(item){
				if (item.show) {
					item.show = false
					item.showText = '展开'
				} else {
					item.show = true
					item.showText = '收起'
				}
			}
			
		}
	}
</script>

<style>

</style>
